﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Collections;

namespace EASerialization.Util
{
    class EAUtil
    {


        /// <summary>
        /// Searches for a TaggedValue with the given name on the given Element
        /// </summary>
        /// <param name="element"></param>
        /// <param name="name"></param>
        /// <returns>the found TaggedValue or null</returns>
        public static EA.TaggedValue findTaggedValue(EA.Element element, String name)
        {
            EA.TaggedValue aTag = null;
            if (element != null)
            {

                element.TaggedValues.Refresh();
                foreach (EA.TaggedValue aTaggi in element.TaggedValues)
                {
                    if (aTaggi.Name == name)
                        aTag = aTaggi;
                }
            }
            return aTag;
        }

        /// <summary>
        /// Searches for a MethodTag with the given name on the given Method
        /// </summary>
        /// <param name="element"></param>
        /// <param name="name"></param>
        /// <returns>the found MethodTag or null</returns>
        public static EA.MethodTag findTaggedValue(EA.Method method, String name)
        {
            method.TaggedValues.Refresh();
            EA.MethodTag aTag = null;

            foreach (EA.MethodTag aTaggi in method.TaggedValues)
            {
                if (aTaggi.Name == name) aTag = aTaggi;
            }

            return aTag;
        }
        /// <summary>
        /// Searches for a ConnectorTag with the given name on the given Connector
        /// </summary>
        /// <param name="element"></param>
        /// <param name="name"></param>
        /// <returns>the found ConnectorTag or null</returns>
        public static EA.ConnectorTag findTaggedValue(EA.Connector element, String name)
        {
            element.TaggedValues.Refresh();
            foreach (EA.ConnectorTag tag in element.TaggedValues)
            {
                if (tag.Name == name)
                    return tag;
            }

            return null;
        }

        public static EA.TaggedValue findTaggedValue(EA.ConnectorEnd connectorend, String name)
        {
            connectorend.TaggedValues.Refresh();
            foreach (EA.TaggedValue tag in connectorend.TaggedValues)
            {
                if (tag.Name == name)
                    return tag;
            }

            return null;
        }

        public static Object findTaggedValue(Object eaObject, String name)
        {
            if (eaObject is EA.Element)
                return findTaggedValue(eaObject as EA.Element, name);
            else if (eaObject is EA.Connector)
                return findTaggedValue(eaObject as EA.Connector, name);
            else if (eaObject is EA.ConnectorEnd)
                return findTaggedValue(eaObject as EA.ConnectorEnd, name);
            else if (eaObject is EA.Package)
                return findTaggedValue(eaObject as EA.Package, name);
            else if (eaObject is EA.Method)
                return findTaggedValue(eaObject as EA.Method, name);
            return null;
        }

        /// <summary>
        /// Searches for a TaggedValue in the packageElement with the given name
        /// </summary>
        /// <param name="element"></param>
        /// <param name="name"></param>
        /// <returns>the found TaggedValue or null</returns>
        public static EA.TaggedValue findTaggedValue(EA.Package package, String name)
        {
            EA.TaggedValue aTag = null;
            EA.Element element = package.Element;
            if (element != null)
            {
                element.TaggedValues.Refresh();


                foreach (EA.TaggedValue aTaggi in element.TaggedValues)
                {
                    if (aTaggi.Name == name) aTag = aTaggi;
                }
            }

            return aTag;
        }
       
        public static EA.TaggedValue setTaggedValue(EA.Repository repository, EA.Package package, String name, String value)
        {
            EA.Package actPackage = package;
            EA.Element element = actPackage.Element;
            if (element != null)
            {
                EA.TaggedValue aTag = findTaggedValue(element, name);

                if (aTag == null) aTag = (EA.TaggedValue)element.TaggedValues.AddNew(name, value);
                aTag.Value = value;
                aTag.Update();
                return aTag;
            }
            return null;
        }

        public static EA.TaggedValue setTaggedValueNotes(EA.Repository repository, EA.Element element, String name, String notes)
        {
            element.TaggedValues.Refresh();
            EA.Element actElement = element;
            EA.TaggedValue aTag = setTaggedValue(repository, actElement, name, "<memo>");
            aTag.Notes = notes;
            aTag.Update();
            return aTag;
        }

        public static EA.TaggedValue setTaggedValueNotes(EA.Repository repository, EA.Package package, String name, String notes)
        {
            if (package.Element != null)
            {
                package.Element.TaggedValues.Refresh();
                EA.TaggedValue aTag = setTaggedValue(repository, package, name, "<memo>");
                aTag.Notes = notes;
                aTag.Update();
                return aTag;
            }
            return null;

        }

        public static Object setTaggedValueNotes(EA.Repository repository, Object eaObject, String name, String notes)
        {
            if (eaObject is EA.Package)
                return setTaggedValueNotes(repository, eaObject as EA.Package, name, notes);
            else if (eaObject is EA.Element)
                return setTaggedValueNotes(repository, eaObject as EA.Element, name, notes);
            else if (eaObject is EA.Connector)
                return setTaggedValueNotes(repository, eaObject as EA.Connector, name, notes);
            else if (eaObject is EA.ConnectorEnd)
                return setTaggedValueNotes(repository, eaObject as EA.ConnectorEnd, name, notes);
            return null;
        }

        public static EA.ConnectorTag setTaggedValueNotes(EA.Repository repository, EA.Connector connector, String name, String notes)
        {
            connector.TaggedValues.Refresh();
            EA.ConnectorTag aTag = setTaggedValue(repository, connector, name, "<memo>");
            aTag.Notes = notes;
            aTag.Update();
            return aTag;
        }

        public static EA.TaggedValue setTaggedValue(EA.Repository repository, EA.Element elem, String name, String value)
        {
            EA.Element element = elem;
            element.TaggedValues.Refresh();
            EA.TaggedValue aTag = findTaggedValue(element, name);
            if (aTag == null) aTag = (EA.TaggedValue)element.TaggedValues.AddNew(name, value);
            aTag.Value = value;
            aTag.Update();
            return aTag;
        }
        /// <summary>
        /// Creates or editExistingLink the ConnectorTag with the given name and value of the given connector
        /// </summary>
        /// <param name="element"></param>
        /// <param name="name"></param>
        /// <param name="value"></param>
        /// <returns>the created or edited ConnectorTag</returns>
        public static EA.ConnectorTag setTaggedValue(EA.Repository repository, EA.Connector con, String name, String value)
        {
            EA.Connector connector = con;
            connector.TaggedValues.Refresh();
            EA.ConnectorTag aTag = findTaggedValue(connector, name);
            if (aTag == null) aTag = (EA.ConnectorTag)connector.TaggedValues.AddNew(name, value);
            aTag.Value = value;
            aTag.Update();
            return aTag;
        }



    }
}
